using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter;
public partial class ViewPatientDetails : System.Web.UI.Page
{
    private const string _menuTdActive = "ViewDetailsLeftMenuTDActive";
    private const string _hyplerlinkActive = "HyperLinkActive";
    private const string _imageUrl = "~/images/PersonalImage.jpg";

    protected void Page_Load(object sender, EventArgs e)
    {
        SetStyles();

        if (!IsPostBack)
        {
            if (Request.QueryString.HasKeys() && Request.QueryString["PatientID"] != "")
            {
                string patientId = Request.QueryString["PatientID"].Trim();
                Patient getPatient = new Patient(Server.MapPath(CreatePatientFilePath()));
                List<Patient> patientDetail = getPatient.SelectPatientRecords("ID='" + patientId + "'");
                if (patientDetail.Count > 0)
                {
                    PopulatePatientBanner(patientDetail);
                    SetViewControls(patientDetail[0].PatientId);
                }

                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                Response.Redirect("Default.aspx", false);
            }
        }
    }

    /// <summary>
    /// Set the style at runtime
    /// </summary>
    private void SetStyles()
    {
        HtmlLink link = new HtmlLink();
        link.Href = "css/TreyResearch.css";
        link.Attributes.Add("type", "text/css");
        link.Attributes.Add("rel", "stylesheet");
        Master.Page.Header.Controls.Add(link);
    }


    /// <summary>
    /// Set the view controls details
    /// </summary>
    /// <param name="patientId">patient id</param>
    private void SetViewControls(Guid patientId)
    {
        ControlHelperClass.ApplicationId = new Guid(Session["ApplicationId"].ToString());
        ControlHelperClass.CCDFolderPath = Server.MapPath(CreateFolderPath(Constants.CcdFolderName));
        ControlHelperClass.CCRFolderPath = Server.MapPath(CreateFolderPath(Constants.CcrFolderName));
        ControlHelperClass.LabResultFolderPath = Server.MapPath(CreateFolderPath(Constants.LabResultsFolderName));
        ControlHelperClass.MedicationFolderPath = Server.MapPath(CreateFolderPath(Constants.MedicationFolderName));
        ControlHelperClass.PatientId = patientId;
    }

    /// <summary>
    /// Populate patient banner user control
    /// </summary>
    /// <param name="patientDetail">List of patient details</param>
    private void PopulatePatientBanner(List<Patient> patientDetail)
    {
        PatientBanner1.Gender = patientDetail[0].Gender.ToString();
        PatientBanner1.FirstName = patientDetail[0].FirstName.ToString();
        PatientBanner1.LastName = patientDetail[0].LastName.ToString();
        PatientBanner1.MiddleName = patientDetail[0].MiddleName.ToString();
        if (!string.IsNullOrEmpty(patientDetail[0].YearOfBirth.ToString()))
        {
            PatientBanner1.YearOfBirth = patientDetail[0].YearOfBirth.ToString();
        }
        else
        {
            PatientBanner1.YearOfBirth = "UnKnown";
        }
        PatientBanner1.State = patientDetail[0].State.ToString();
        PatientBanner1.City = patientDetail[0].City.ToString();
        PatientBanner1.Country = patientDetail[0].Country.ToString();
        PatientBanner1.Delimiter = ",";
        if (File.Exists(Server.MapPath(CreateImagePath(patientDetail[0].RecordId.ToString()))))
        {
            PatientBanner1.ImagePath = CreateImagePath(patientDetail[0].RecordId.ToString());
        }
        else
        {
            PatientBanner1.ImagePath = _imageUrl;
        }

    }


    protected void LinkButtonCCRs_Click(object sender, EventArgs e)
    {
        ResetStyles();
        PanelCCR.CssClass = _menuTdActive;
        LinkButtonCCR.CssClass = "HyperLinkActive";
        MultiView1.ActiveViewIndex = 1;
    }
    protected void LinkButtonMedication_Click(object sender, EventArgs e)
    {

        ResetStyles();
        PanelMedication.CssClass = _menuTdActive;
        LinkButtonMedication.CssClass = _hyplerlinkActive;
        MultiView1.ActiveViewIndex = 0;

    }
    protected void LinkButtonLabTest_Click(object sender, EventArgs e)
    {
        ResetStyles();
        PanelLabTest.CssClass = _menuTdActive;
        LinkButtonLabTest.CssClass = _hyplerlinkActive;
        MultiView1.ActiveViewIndex = 3;


    }
    protected void LinkButtonCCDs_Click(object sender, EventArgs e)
    {
        ResetStyles();
        PanelCCD.CssClass = _menuTdActive;
        LinkButtonCCD.CssClass = _hyplerlinkActive;
        MultiView1.ActiveViewIndex = 2;
    }

    /// <summary>
    ///  Reset style for Left menu panel and Link buttons
    /// </summary>       
    protected void ResetStyles()
    {
        string leftMenuTD = "ViewDetailsLeftMenuTD";
        string hyperlink = "HyperLink";

        PanelCCD.CssClass = leftMenuTD;
        PanelLabTest.CssClass = leftMenuTD;
        PanelMedication.CssClass = leftMenuTD;
        PanelCCR.CssClass = leftMenuTD;
        LinkButtonCCD.CssClass = hyperlink;
        LinkButtonLabTest.CssClass = hyperlink;
        LinkButtonMedication.CssClass = hyperlink;
        LinkButtonCCR.CssClass = hyperlink;
    }
    /// <summary>
    /// Creates file path
    /// </summary>
    /// <returns></returns>
    private string CreatePatientFilePath()
    {
        return "Data/" + Session["PracticeFolderName"].ToString() + "/" + Constants.PatientXmlFileName;
    }
    /// <summary>
    /// Creates image url
    /// </summary>
    /// <param name="fileName">image file name</param>
    /// <returns>image url</returns>
    private string CreateImagePath(string fileName)
    {
        return "~/Data/" + Session["PracticeFolderName"] + "/" + fileName + ".jpg";
    }
    /// <summary>
    /// Create folder path
    /// </summary>
    /// <param name="folderName">folder name</param>
    /// <returns>folder path</returns>
    private string CreateFolderPath(string folderName)
    {
        return "Data/" + Session["PracticeFolderName"] + "/" + folderName;
    }
}
